package com.bw.module_home.ui

import android.app.Activity
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.ContextThemeWrapper
import android.view.Gravity
import android.view.View
import android.widget.PopupMenu
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.bw.lib_common.arouter.Router
import com.bw.lib_common.base.BaseActivity
import com.bw.module_home.R
import com.bw.module_home.databinding.ActivityRepairBinding
import com.bw.module_home.repair.model.AddRepairEntity
import com.bw.module_home.repair.viewmodel.RepairViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/repair/add_repair")
class RepairActivity : BaseActivity<RepairViewModel, ActivityRepairBinding>() {
    override val mLayoutResId: Int = R.layout.activity_repair
    private val viewModel: RepairViewModel by viewModels()

    @RequiresApi(Build.VERSION_CODES.M)
    override fun initView() {
        super.initView()
        mBinding.repairBack.setOnClickListener {
            finish()
        }
        mBinding.repairSubmit.setOnClickListener {
            val repairEntityItem = AddRepairEntity(
                id = 0,
                userId = 443,
                orderNumber = mBinding.repairNumber.text.toString(),
                repairName = mBinding.repairName.text.toString(),
                contactMethod = 13235148892,
                repairLocation = 1,
                processingStatus = "待处理",
                repairTime = System.currentTimeMillis().toString(),
                faultDescription = mBinding.repairDesc.text.toString(),
                faultPhotos = "http://example.com/photo1.jpg,http://example.com/photo2.jpg"
            )
            viewModel.addRepair(repairEntityItem)
        }
//        mBinding.addImg2.setOnClickListener {
//            PictureSelector.create(this, 512).selectPicture()
//        }
        val wrapper =
            ContextThemeWrapper(this, R.style.CustomPopupMenu)
        val popupMenu = PopupMenu(wrapper, mBinding.repairLocationArrow)
        popupMenu.menuInflater.inflate(R.menu.location_menu, popupMenu.menu)
        popupMenu.gravity = Gravity.BOTTOM
        mBinding.repairLocationArrow.setOnClickListener {
            popupMenu.show()
        }
        popupMenu.setOnMenuItemClickListener {
            mBinding.repairLocation.setText(it.title.toString())
            false
        }
    }

    override fun initData() {
        super.initData()
        observeDataChange()
    }

    private fun observeDataChange() {
        viewModel.addRepairSuccess.observe(this) {
            Toast.makeText(this, "提交成功", Toast.LENGTH_SHORT).show()
//            ToastUtils.showLong("提交成功")
            finish()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 512 && resultCode == Activity.RESULT_OK) {
            val extras = data?.extras
//            val pictureBean = extras?.get(PictureSelector.PICTURE_RESULT) as? PictureBean
//            pictureBean?.path?.let { path ->
//                mBinding.addImg.visibility = View.VISIBLE
//                Glide.with(this).load(path).into(mBinding.addImg)
//            }
        }
    }
}